package com.biostime.dingdingbox.modules.db.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.VideoRecord;
import com.biostime.dingdingbox.modules.db.service.VideoRecordService;

/**
 * 视频记录表Controller
 * @author chenweicheng
 * @version 2016-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/db/videoRecord")
public class VideoRecordController extends BaseController {

	@Autowired
	private VideoRecordService videoRecordService;
	
	@ModelAttribute
	public VideoRecord get(@RequestParam(required=false) String id) {
		VideoRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = videoRecordService.get(id);
		}
		if (entity == null){
			entity = new VideoRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("db:videoRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(VideoRecord videoRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VideoRecord> page = videoRecordService.findPage(new Page<VideoRecord>(request, response), videoRecord); 
		model.addAttribute("page", page);
		return "modules/db/videoRecordList";
	}

	@RequiresPermissions("db:videoRecord:view")
	@RequestMapping(value = "form")
	public String form(VideoRecord videoRecord, Model model) {
		model.addAttribute("videoRecord", videoRecord);
		return "modules/db/videoRecordForm";
	}

	@RequiresPermissions("db:videoRecord:edit")
	@RequestMapping(value = "save")
	public String save(VideoRecord videoRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, videoRecord)){
			return form(videoRecord, model);
		}
		videoRecordService.save(videoRecord);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/db/videoRecord/?repage";
	}
	
	@RequiresPermissions("db:videoRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, VideoRecord videoRecord, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				videoRecordService.delete(new VideoRecord(id));
			}
		}else{
			videoRecordService.delete(videoRecord);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/db/videoRecord/?repage";
	}

}